package com.hsnn.medstgmini.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.hsnn.medstgmini.service.CommonService;
import com.hsnn.medstgmini.sys.model.UserType;

/**
 * @category 用户帐号生成器
 * @author 蔡春龙
 * @date 2015年6月17日
 */
public class AccountUtil {

	private static CommonService commonService;

	private AccountUtil() {
	}

	/**
	 * @category 生成主帐号
	 * @date 2015年10月16日
	 * @param userType
	 * @return
	 */
	public static synchronized String getSequence(int userType) {
		if (commonService == null) {
			WebApplicationContext wac = ContextLoader
					.getCurrentWebApplicationContext();
			commonService = wac.getBean(CommonService.class);
		}
		Long sequence = commonService.getSequence(userType);
		return getPrefix(userType) + sequence;
	}

	/**
	 * @category 生成子帐号
	 * @date 2015年10月16日
	 * @param userType
	 * @return
	 */
	public static synchronized String getSubSequence(String mainAccount) {
		if (commonService == null) {
			WebApplicationContext wac = ContextLoader
					.getCurrentWebApplicationContext();
			commonService = wac.getBean(CommonService.class);
		}
		Long sequence = commonService.getSubSequence(mainAccount);
		return mainAccount + "_" + sequence;
	}

	/**
	 * @category 帐号前缀
	 * @date 2015年10月16日
	 * @param userType
	 * @return
	 */
	private static String getPrefix(int userType) {
		String username = "P";
		if (userType == UserType.zx.getKey()) {
			username = "Z";
		} else if (userType == UserType.sc.getKey()) {
			username = "S";
		} else if (userType == UserType.ps.getKey()) {
			username = "GZPS";
		} else if (userType == UserType.yy.getKey()) {
			username = "Y";
		} else if (userType == UserType.zj.getKey()) {
			username = "J";
		} else if (userType == UserType.hszx.getKey()) {
			username = "W";
		}
		return username;
	}

	
	/**
	 * @category 生成项目编号
	 * @date 2015年11月11日13:57:51
	 * @param userType
	 * @return
	 */
	public static synchronized String getProjSequence(String province) {
		if (commonService == null) {
			WebApplicationContext wac = ContextLoader
					.getCurrentWebApplicationContext();
			commonService = wac.getBean(CommonService.class);
		}
		Long sequence = commonService.getSequence(7);
		return  province + sequence;
	}

	/**
	 * @category 生成基础库编码
	 * @date 2015年11月11日13:57:51
	 * @param userType
	 * @return
	 */
	public static synchronized String getStdSequence(String prefix, int stdType, int subFixLen) {
		if (commonService == null) {
			WebApplicationContext wac = ContextLoader
					.getCurrentWebApplicationContext();
			commonService = wac.getBean(CommonService.class);
		}
		Long sequence = commonService.getSequence(stdType);
		String strSequece = StringUtils.leftPad("" + sequence, subFixLen, '0');
		return  prefix + strSequece;
	}
	
	/**
	 * @category 生成登陆帐号
	 * @date 2015年6月17日
	 * @param type
	 * @param number
	 * @return
	 */
	public static String generate(UserType type, Long number) {
		String username = "P";
		if (type == UserType.zx) {
			username = "Z" + number;
		} else if (type == UserType.sc) {
			username = "S" + number;
		} else if (type == UserType.ps) {
			username = "P" + number;
		} else if (type == UserType.yy) {
			username = "Y" + number;
		} else if (type == UserType.zj) {
			username = "J" + number;
		} else if (type == UserType.hszx) {
			username = "W" + number;
		}
		return username;
	}

	public static String[] letter = { "a", "b", "c", "d", "e", "f", "g", "h",
			"i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w",
			"x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
			"K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
			"Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

	
	public static String[] upperCase ={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
									   "M","N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
	public static String[] lowerCase ={"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 
									   "m","n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
	public static String[] number = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	public static String[] special ={ "~", "!", "@", "#", "$",  "%", "^", "&", "*",
							          "(", ")", "_", "+", "\\", "-", "=", "{", "}",
							          ":", "'", "<", ">", "?",  ",", ".", "/" };
	
	/**
	 * @category 随机生成密码
	 * @date 2015年10月27日15:15:41
	 * @author 应晓川
	 * @param len
	 * @return
	 */
	public static String getPassword(int len) {
		StringBuffer password = new StringBuffer();
		for (int i = 0; i < len; i++) {
			int index=0;
			String temp="";
//			if(i==0){
//				 index = (int) Math.floor(Math.random() * upperCase.length);
//				 temp = upperCase[index];
//			}
//			if(i==1){
//				 index = (int) Math.floor(Math.random() * lowerCase.length);		
//				 temp = lowerCase[index];
//			}
//			if(i==2){
//				 index = (int) Math.floor(Math.random() * number.length);
//				 temp = number[index];
//			}
//			if(i==3){
//				 index = (int) Math.floor(Math.random() * special.length);
//				 temp = special[index];
//			}
//			if(4<=i){
//				index = (int) Math.floor(Math.random() * letter.length);
//				temp = letter[index];
//			}
			 index = (int) Math.floor(Math.random() * number.length);
			 temp = number[index];
			password.append(temp);
		}
	    char[] passwordes  = password.toString().toCharArray();
	    password.setLength(0);
	    List<Character> list = new ArrayList<Character>();    
	    for(int i = 0;i < passwordes.length;i++){    
            list.add(passwordes[i]);    
        }  
        Collections.shuffle(list);//密码乱序  
        for(char c: list){
        	password.append(c);
        }
		return password.toString();
	}
	
}
